package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func main() {
	r := gin.Default()
	err := r.SetTrustedProxies([]string{"127.0.0.1"})
	if err != nil {
		fmt.Println(err)
	}
	r.GET("/ping", HandleError(handle))
	s := &http.Server{
		Addr:           "127.0.0.1:8080",
		Handler:        r,
		ReadTimeout:    20 * time.Second,
		WriteTimeout:   20 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	if err = s.ListenAndServe(); err != nil {
		fmt.Println(err.Error())
	}
}

func handle(c *gin.Context) error {
	c.String(http.StatusOK, "pong")
	return nil
}

type GinFunc func(c *gin.Context) error

func HandleError(GinFuncErr GinFunc) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		defer func() {
			err := recover()
			if err != nil {
				fmt.Println(err)
			} else {
				fmt.Println("eeeee")
			}
		}()
		err := GinFuncErr(ctx)
		if err != nil {
			/*TODO 错误处理*/
		}
		panic("dddd")
	}
}
